package com.jackiechancinema.ssm.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.EmpBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.CustomerDao;
import com.jackiechancinema.ssm.dao.EmpDao;
import com.jackiechancinema.ssm.dto.CustomerDto;
import com.jackiechancinema.ssm.dto.EmpExcelDto;
import com.jackiechancinema.ssm.entity.Customer;
import com.jackiechancinema.ssm.entity.Emp;
import com.jackiechancinema.ssm.listener.EmpReadListener;
import com.jackiechancinema.ssm.service.Inte.EmpService;
import com.jackiechancinema.ssm.utils.BeanUtil;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Autowired
    private EmpReadListener listener;

    //新增
    @Override
    public R insert(Emp emp) {
        if (empDao.insert(emp)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }

    }
    //查询
    @Override
    public PageVo<Emp> queryPage(EmpBo empBo) {
        if (empBo != null){
            Emp emp = BeanUtil.copyProperty(Emp.class, empBo, empBo.getClass().getDeclaredFields());
            PageHelper.startPage(empBo.getPage(), empBo.getLimit());//开启分页
            PageInfo<Emp> pageInfo = new PageInfo<Emp>(empDao.selectAll(emp));
            return new PageVo<Emp>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());
        }
        return new PageVo<Emp>(SystemContant.PAGE_R,"OK",0,null);
    }
    //删除
    @Override
    public R deleteById(int id) {
        if (empDao.delete(id)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    //批量删除
    @Override
    public R deleteBatch(List<Emp> emps) {

        List<Integer> list = new ArrayList<>();
        for (Emp emp : emps) {
            list.add(emp.getEmpno());
        }

        if (empDao.batchDel(list)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }
    @Override
    public R update(Emp emp) {
        if (empDao.update(emp)>0){
            return R.ok(null);
        }else{
            return R.fail();
        }
    }

    @Override
    public R insertBatch(MultipartFile file) {
        if (!file.isEmpty()){
            try {
                EasyExcel.read(file.getInputStream(), EmpExcelDto.class,listener).sheet().doRead();
                return R.ok("OK");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return R.fail();
    }

    @Override
    public void exportExcel(EmpBo bo, HttpServletResponse response) {
        //1.设置响应消息头，目的：告诉请求方，这个是下载
        String fn=System.currentTimeMillis()+".xlsx";
        //设置响应头  告知浏览器，要以附件的形式保存内容   filename=浏览器显示的下载文件名
        response.setHeader("content-disposition","attachment;filename="+fn);

        //2.组装查询条件
        Emp emp=new Emp();
        if(StringUtils.hasLength(bo.getEname())){
            emp.setEname(bo.getEname());
        }
        if(StringUtils.hasLength(bo.getPhone())){
            emp.setPhone(bo.getPhone());
        }
        if(bo.getType()>-1){
            emp.setType(bo.getType());
        }
        if(StringUtils.hasLength(bo.getEducation())){
            emp.setEducation(bo.getEducation());
        }
        if(bo.getHiredate() != null){
            emp.setHiredate(bo.getHiredate());
        }
        //3.获取要下载的数据
        List<Emp> list=empDao.selectAll(emp);
        try {
            //4.生成Excel，并将数据写出到响应流中
            EasyExcel.write(response.getOutputStream(),Emp.class).sheet().doWrite(list);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
